 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.activity
 * File     : Base.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2011-11-11
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.activity;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.utils.Globals;
import net.solosky.easyavatar.utils.Views;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * 所有Activity的抽象父类，提供一些共同的操作
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public abstract class Base extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Views.setContext(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Views.setContext(this);
	}
	
	/**
	 * 设置当前activity标题
	 * @param title
	 */
	public void setTitle(int stringId){
		TextView tvText = (TextView) findViewById(R.id.title);
		tvText.setText(stringId);
	}
	
	public void setTitle(String title){
		TextView tvText = (TextView) findViewById(R.id.title);
		tvText.setText(title);
	}
	
	/**
	 * 设置左侧按钮
	 * @param btnTextId
	 * @param onLeftClick
	 */
	public void setLeftButton(int btnTextId, View.OnClickListener onLeftClick){
		Button btnLeft = (Button) findViewById(R.id.bt_title_left);
		btnLeft.setText(btnTextId);
		btnLeft.setOnClickListener(onLeftClick);
		btnLeft.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 设置右侧按钮
	 * @param btnTextId
	 * @param onRightClick
	 */
	public void setRightButton(int btnTextId, View.OnClickListener onRightClick){
		Button btnLeft = (Button) findViewById(R.id.bt_title_right);
		btnLeft.setText(btnTextId);
		btnLeft.setOnClickListener(onRightClick);
		btnLeft.setVisibility(View.VISIBLE);
	}
	
	/**
	 * 设置左侧按钮为返回按钮
	 */
	public void setupBackButton(){
		setLeftButton(R.string.back, 
				new View.OnClickListener() {
					public void onClick(View v) {
						finish();
					}
				}
		);
	}
	
	/**
	 * 判断当前是否初始化，如果没有跳转到欢迎页面初始化
	 */
	public boolean checkInitilized(){
		if( !Globals.IS_INITILIZED || Globals.PROVIDER_META_LIST==null ){
			Intent intent = getIntent();
			intent.putExtra("welcome.nextIntent", this.getClass().getName());
			intent.setClass(this, Welcome.class);
			startActivity(intent);
			return false;
		}else{
			return true;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		 //MenuInflater inflater = this.getMenuInflater();   
		 //inflater.inflate(R.menu.main, menu);   
		 return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		/*switch (item.getItemId()) {
		//case R.id.option_about:
			//startActivity(new Intent(this, About.class));
			break;
			
		case R.id.option_exit:
			System.exit(0);
			break;
		}*/
		return true;
	}
}
